home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10941 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Fun with <time.h>
  5. Date: Wed, 20 Mar 96 21:45:10 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <827358310snz@genesis.demon.co.uk>
  9. References: <Pine.A32.3.91.960318145539.118335A-100000@red.weeg.uiowa.edu>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <Pine.A32.3.91.960318145539.118335A-100000@red.weeg.uiowa.edu>
  16.            robinson@blue.weeg.uiowa.edu "The Amorphous Mass" writes:
  17.  
  18. >
  19. >  I'm writing a little routine that takes a date, a format to parse the 
  20. >date with, and a format to convert the date to, and so I've been sniffing 
  21. >around in <time.h>.  One minor annoyance has been that if I'm given a 
  22. >date like 5/6/95 and a format that wants the day (Monday, Tuesday, etc) 
  23. >I couldn't find any functionality to do that.  But someone noted in 
  24. >comp.lang.c.moderated that it was possible to pass an incomplete struct 
  25. >tm to mktime() and mktime() would "fill in the blanks" if there was 
  26. >enough information.  Is this true, or am I going to have to join the 
  27. >vast and lonely horde of people looking for perpetual calendar 
  28. >algorithms? :-)
  29.  
  30. mktime() will do what you want in this case. It ignores the original
  31. values of tm_wday and tm_yday and fills them in calculated from the
  32. other members of the structure.
  33.  
  34. >  On a side note, what does happen if you pass an incompletely filled 
  35. >struct tm (ie, some fields set to 0) to mktime()?  I got some pretty 
  36. >colorful results from DEC C...
  37.  
  38. You *must* set all of the (standard) struct tm members other than tm_wday
  39. and tm_yday appropriately before calling mktime(). The values need not
  40. be within any specific bounds in which case mktime() will adjust the values
  41. of all of the members to bring them within appropriate ranges, while
  42. maintaining the same point in time. So, for example, if you want to determine
  43. what the date is tomorrow you can:
  44.  
  45. ...
  46.     time_t timeval;
  47.     struct tm *tmptr;
  48.  
  49.     timeval = time(NULL);
  50.     tmptr = localtime(&timeval);
  51.     tmptr->tm_mday++;
  52.     tmptr->is_dst = -1;  /* This allows it to choose an appropriate
  53.                             daylight-saving-time state and preserve the
  54.                             hour value, if possible */
  55.     mktime(tmptr);
  56.  
  57. And now all of the struct tm fields are set up appropriately for this time
  58. tomorrow.
  59.  
  60. -- 
  61. -----------------------------------------
  62. Lawrence Kirby | fred@genesis.demon.co.uk
  63. Wilts, England | 70734.126@compuserve.com
  64. -----------------------------------------
  65.